﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ArtOfTest.WebAii.Controls.HtmlControls;

namespace SM2SetupSearch
{
    public class GlobalExcludeActions
    {
        public static void AddPhrase(string strPhrase)
        {
                Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
                HtmlDiv cphPageContent_pnlGlobalExclude = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("cphPageContent_pnlGlobalExclude");
                HtmlInputText cphPageContent_tbGlobalExcludeWord = cphPageContent_pnlGlobalExclude.Find.ById<HtmlInputText>("cphPageContent_tbGlobalExcludeWord");
                SM2Input.Tradition.TypeTextIntoTextFieldWithHint(cphPageContent_tbGlobalExcludeWord, strPhrase);

                HtmlInputSubmit cphPageContent_btnAddGlobalExclude = cphPageContent_pnlGlobalExclude.Find.ById<HtmlInputSubmit>("cphPageContent_btnAddGlobalExclude");
                SM2Input.Tradition.ClickButton(cphPageContent_btnAddGlobalExclude);
        }

        public static void RemoveAllPhrase()
        {
            Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
            HtmlDiv cphPageContent_pnlGlobalExclude = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("cphPageContent_pnlGlobalExclude");

            HtmlDiv cphPageContent_pnlGlobalExcludeWords = cphPageContent_pnlGlobalExclude.Find.ById<HtmlDiv>("cphPageContent_pnlGlobalExcludeWords");
            HtmlTable cphPageContent_gvGlobalExcludeWords = cphPageContent_pnlGlobalExcludeWords.Find.ById<HtmlTable>("cphPageContent_gvGlobalExcludeWords");


            HtmlInputCheckBox cbSelectAll = cphPageContent_gvGlobalExcludeWords.Find.ById<HtmlInputCheckBox>("cbSelectAll");
            cbSelectAll.Click();

            HtmlInputSubmit cphPageContent_btnRemoveSelectedGEW = cphPageContent_pnlGlobalExclude.Find.ById<HtmlInputSubmit>("cphPageContent_btnRemoveSelectedGEW");
            SM2Input.Tradition.ClickButton(cphPageContent_btnRemoveSelectedGEW);
        }

        public static void RemovePhrase(string strPhrase)
        {
            Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
            HtmlDiv cphPageContent_pnlGlobalExclude = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("cphPageContent_pnlGlobalExclude");
            
            HtmlDiv cphPageContent_pnlGlobalExcludeWords = cphPageContent_pnlGlobalExclude.Find.ById<HtmlDiv>("cphPageContent_pnlGlobalExcludeWords");
            HtmlTable cphPageContent_gvGlobalExcludeWords = cphPageContent_pnlGlobalExcludeWords.Find.ById<HtmlTable>("cphPageContent_gvGlobalExcludeWords");

            int intCountRows = cphPageContent_gvGlobalExcludeWords.Rows.Count;
            for (int i = 1; i < intCountRows; i++)
            {
                HtmlTableRow tblrow = cphPageContent_gvGlobalExcludeWords.AllRows[i];
                HtmlTableCell tblCell_Words = tblrow.Cells[2];

                string strCell_SearchWords = tblCell_Words.TextContent.Trim();
                if (strCell_SearchWords.Contains(strPhrase))
                {
                    HtmlTableCell tblCell_Checkbox = tblrow.Cells[0];
                    IList<HtmlInputCheckBox> iListControls = tblCell_Checkbox.Find.AllByAttributes<HtmlInputCheckBox>();
                    HtmlInputCheckBox checkbox = iListControls[0];
                    tblCell_Checkbox.Click();
                    break;
                }
            }

            HtmlInputSubmit cphPageContent_btnRemoveSelectedGEW = cphPageContent_pnlGlobalExclude.Find.ById<HtmlInputSubmit>("cphPageContent_btnRemoveSelectedGEW");
            SM2Input.Tradition.ClickButton(cphPageContent_btnRemoveSelectedGEW);
        }

        public static void Confirm(string strConfirm)
        {
            Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
            HtmlDiv cphPageContent_pnlGlobalExclude = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("cphPageContent_pnlGlobalExclude");
            
            //Final action Apply or Cancel
            if (strConfirm.Equals("yes"))
            {
                HtmlInputSubmit cphPageContent_btnSaveGlobalExclude = cphPageContent_pnlGlobalExclude.Find.ById<HtmlInputSubmit>("cphPageContent_btnSaveGlobalExclude");
                SM2Input.Tradition.ClickButton(cphPageContent_btnSaveGlobalExclude);
            }
            else
            {
                HtmlInputSubmit cphPageContent_btnCloseGlobalExclude = cphPageContent_pnlGlobalExclude.Find.ById<HtmlInputSubmit>("cphPageContent_btnCloseGlobalExclude");
                SM2Input.Tradition.ClickButton(cphPageContent_btnCloseGlobalExclude);
            }
        }        
    }
}
